package com.yougou.ui.cart;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.yougou.base.BaseViewModel;
import com.yougou.data.model.CartItem;
import com.yougou.utils.CartManager;
import com.yougou.utils.UserManager;

import java.util.List;

public class CartViewModel extends BaseViewModel {
    
    private MutableLiveData<List<CartItem>> cartItems = new MutableLiveData<>();
    private MutableLiveData<Double> totalPrice = new MutableLiveData<>();
    private MutableLiveData<Boolean> allChecked = new MutableLiveData<>();
    private MutableLiveData<String> message = new MutableLiveData<>();
    private MutableLiveData<Boolean> needLogin = new MutableLiveData<>();
    private MutableLiveData<List<CartItem>> checkoutData = new MutableLiveData<>();
    
    public LiveData<List<CartItem>> getCartItems() {
        return cartItems;
    }
    
    public LiveData<Double> getTotalPrice() {
        return totalPrice;
    }
    
    public LiveData<Boolean> getAllChecked() {
        return allChecked;
    }
    
    public LiveData<String> getMessage() {
        return message;
    }
    
    public LiveData<Boolean> getNeedLogin() {
        return needLogin;
    }
    
    public LiveData<List<CartItem>> getCheckoutData() {
        return checkoutData;
    }
    
    
    public void loadCartData() {
        List<CartItem> items = CartManager.getCartItems();
        cartItems.setValue(items);
        updateTotalPrice();
        updateAllChecked();
    }
    
    
    public void updateItemChecked(CartItem item, boolean checked) {
        CartManager.updateChecked(item.getGoods().getGoodsId(), checked);
        updateTotalPrice();
        updateAllChecked();
    }
    
    
    public void updateItemQuantity(CartItem item, int quantity) {
        CartManager.updateQuantity(item.getGoods().getGoodsId(), quantity);
        loadCartData();
    }
    
    
    public void toggleAllChecked(boolean checked) {
        CartManager.updateAllChecked(checked);
        loadCartData();
    }
    
    
    public void deleteCheckedItems() {
        int count = CartManager.getCheckedCount();
        if (count == 0) {
            message.setValue("请选择要删除的商品");
            return;
        }
        CartManager.removeCheckedItems();
        loadCartData();
        message.setValue("已删除 " + count + " 件商品");
    }
    
    
    public void checkout() {
        // 检查登录状态
        if (!UserManager.isLogin()) {
            needLogin.setValue(true);
            message.setValue("请先登录");
            return;
        }
        
        int count = CartManager.getCheckedCount();
        if (count == 0) {
            message.setValue("请选择要结算的商品");
            return;
        }
        
        // 获取选中的商品列表
        List<CartItem> items = CartManager.getCartItems();
        List<CartItem> checkedItems = new java.util.ArrayList<>();
        for (CartItem item : items) {
            if (item.isChecked()) {
                checkedItems.add(item);
            }
        }
        
        // 触发结算跳转
        checkoutData.setValue(checkedItems);
    }
    
    
    private void updateTotalPrice() {
        double total = CartManager.getCheckedTotal();
        totalPrice.setValue(total);
    }
    
    
    private void updateAllChecked() {
        boolean checked = CartManager.isAllChecked();
        allChecked.setValue(checked);
    }
}

